package com.haima.sage.bigdata.api.frame.condition.expression.impl;

import java.util.Date;
import java.util.regex.Pattern;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.haima.sage.bigdata.api.frame.condition.expression.BaseLogicService;

public class RegexLogicService extends BaseLogicService {
    private static final Logger LOG = LogManager.getLogger(RegexLogicService.class);

    private Pattern pattern;

    public RegexLogicService(Object param) {
        super(param);
        try {
            this.pattern = Pattern.compile(this.strParam);
        } catch (Exception e) {
            LOG.error("pattern compile fail, regex:{}", this.param, e);
        }
    }

    @Override
    public boolean logicNotEmpty(String object) throws Exception {
        return pattern.matcher(object).matches();
    }

    @Override
    public boolean logicNotEmpty(Boolean object) throws Exception {
        return pattern.matcher(object.toString()).matches();
    }

    @Override
    public boolean logicNotEmpty(Character object) throws Exception {
        return pattern.matcher(object.toString()).matches();
    }

    @Override
    public boolean logicNotEmpty(Byte object) throws Exception {
        return pattern.matcher(object.toString()).matches();
    }

    @Override
    public boolean logicNotEmpty(Short object) throws Exception {
        return pattern.matcher(object.toString()).matches();
    }

    @Override
    public boolean logicNotEmpty(Integer object) throws Exception {
        return pattern.matcher(object.toString()).matches();
    }

    @Override
    public boolean logicNotEmpty(Long object) throws Exception {
        return pattern.matcher(object.toString()).matches();
    }

    @Override
    public boolean logicNotEmpty(Float object) throws Exception {
        return pattern.matcher(object.toString()).matches();
    }

    @Override
    public boolean logicNotEmpty(Double object) throws Exception {
        return pattern.matcher(object.toString()).matches();
    }

    @Override
    public boolean logicNotEmpty(Date object) throws Exception {
        return pattern.matcher(sdf.format(object)).matches();
    }


}
